# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from skimage import data,color
import numpy as np

#定义灰度值到彩色变换
L=255
def GetR(gray):
    if gray<L/2:
        return 0
    elif gray>L/4*3:
        return L
    else:
        return 4*gray-2*L

def GetG(gray):
    if gray<L/4:
        return 4*gray
    elif gray>L/4*3:
        return 4*L-4*gray
    else:
        return L

def GetB(gray):
    if gray<L/4:
        return L
    elif gray>L/2:
        return 0
    else:
        return 2*L-4*gray

#灰度映射伪彩图
def main(img):
    grayimg=color.rgb2gray(img)*255#将彩色图像转化为灰度图像
    colorimg=np.zeros(img.shape,dtype='uint8')
    for ii in range(img.shape[0]):
        for jj in range(img.shape[1]):
            r,g,b=GetR(grayimg[ii,jj]),GetG(grayimg[ii,jj]),GetB(grayimg[ii,jj])
            colorimg[ii,jj,:]=[r,g,b]
    return grayimg,colorimg
    

#设置字体格式
#
#plt.rcParams['font.sans-serif']=['SimHei']
#plt.rcParams['font.size']=15
#plt.rcParams['axes.unicode_minus']=False
#
#x=[0,64,127,191,255]
##绘制灰度图像到R通道的映射关系
#plt.figure()
#R=[]
#for i in x:
#    R.append(GetR(i))
#plt.plot(x,R,'r--',label='红色变换')
#plt.legend(loc='best')
#
##绘制灰度图像到G通道的映射关系
#plt.figure()
#G=[]
#for i in x:
#    G.append(GetG(i))
#plt.plot(x,G,'g',label='绿色变换')
#plt.legend(loc='best')
#
##绘制灰度图像到B通道的映射关系
#plt.figure()
#B=[]
#for i in x:
#    B.append(GetB(i))
#plt.plot(x,B,'b',marker='o',markersize=5,label='蓝色变换')
#plt.legend(loc='best')
#
#
##绘制灰度图像到RGB的映射
#plt.figure()
#plt.plot(x,R,'r--')
#plt.plot(x,G,'g')
#plt.plot(x,B,'b',marker='o',markersize=5)
#
#
#
##灰度图按照映射关系转化为伪彩图
#img=data.coffee()
#gh=main(img)         
# #显示图像
#
#plt.figure()
#plt.axis('off')
#plt.imshow(gh[0],cmap='gray')
#plt.figure()
#plt.axis('off')
#plt.imshow(gh[1])#显示伪彩色图像
#

